package com.tbit.common.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import com.tbit.common.entity.GPS.CarHistory;
import com.tbit.common.entity.GPS.GPSCar;
import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;
import java.util.Collections;
import java.util.List;

@Slf4j
public class GPSUtil {
    public static String name = "软件二部测试";
    public static String psw = "rj123456";
    public static String key = "7dfe6d68-bce8-42c4-8e54-6e166e35ed9a";

    /**
     * GPS登陆接口
     *
     */
    public static String login() throws Exception {
        String t = String.valueOf(getT());
        String sign = getSign(t, psw);

        try (HttpResponse response = HttpRequest.get("http://api.tbitgps.com/account/login.do")
                .form("login_name", name)
                .form("app_key", key)
                .form("sign", sign)
                .form("t", t)
                .execute()) {
            if (response.isOk()) {
                String content = response.body();
                JSONObject jo = JSONObject.parseObject(content);
                log.info("GPS查车登陆接口：{}", jo);
                return jo.getString("data");
            }
        }
        return "错误";
    }

    /**
     * 获取最后位置
     *
     */
    public static List<CarHistory> findlastpos(String token, String mnos) {
        try (HttpResponse response = HttpRequest.get("http://api.tbitgps.com/device/findlastpos.do")
                .form("device_nos", mnos)
                .form("map_type", "1")
                .header("access_token", token)
                .header("Content-Type", "application/x-www-form-urlencoded")
                .execute()) {
            if (response.isOk()) {
                String content = response.body();
                GPSCar gpsCar = JSONObject.parseObject(content, GPSCar.class);
                if (gpsCar.getRet().equals(1)) {
                    log.info("获取GPS查车最后位置成功！");
                    return gpsCar.getData();
                } else {
                    log.info("获取GPS查车最后位置失败！返回：" + content);
                }
            }
        }
        return Collections.emptyList();
    }

    /**
     * 获取历史轨迹
     *
     */
    public static List<CarHistory> findhistorytrack(String token, String mnos, String begin_time, String end_time) {
        try (HttpResponse response = HttpRequest.get("http://api.tbitgps.com/history/findhistorytrack.do")
                .form("device_no", mnos)
                .form("map_type", "1")
                .form("begin_time", begin_time)
                .form("end_time", end_time)
                .header("access_token", token)
                .header("Content-Type", "application/x-www-form-urlencoded")
                .execute()) {
            if (response.isOk()) {
                String content = response.body();
                GPSCar gpsCar = JSONObject.parseObject(content, GPSCar.class);
                if (gpsCar.getRet().equals(1)) {
                    log.info("GPS查车获取历史轨迹成功！");
                    return gpsCar.getData();
                } else {
                    log.info("GPS查车获取历史轨迹失败！返回：{},mnos:{},begin_time:{},end_time:{},map_type:{}", content,mnos,begin_time,end_time,"1");
                }
            }
        }
        return Collections.emptyList();
    }

    public static long getT() {
        return System.currentTimeMillis();
    }

    public static String MD5(String s) {
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        try {
            byte[] btInput = s.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;
            for (byte byte0 : md) {
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    public static String getSign(String t, String password) {
        return MD5(password + t);
    }

    public static void main(String[] args) throws Exception {
        String token = login();
        System.out.println(token);
        System.out.println(findlastpos(token, "007420007"));
        findhistorytrack(token,"004091967","2021-04-16 10:00:00","2021-04-16 18:00:00");
    }
}